package in.muton.edushare.pages.publicpages.userpages;

import in.muton.edushare.pages.publicpages.userpages.ProfileTopMenu.MenuItems;

import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.RequestConstants;
import com.edushare.common.dao.ProfessionalDetailsDAO;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.ProfessionalDetails;

public class ProfessionalProfile extends UserProfileBase {

	private boolean isEduTab = false;
	
	public ProfessionalProfile(PageParameters params) {
		super(params);
		String tabName = params.getString("1", RequestConstants.PARAM_USR_EXPERIENCES);
		if(tabName.equalsIgnoreCase(RequestConstants.PARAM_USR_EDUCATION)){
			isEduTab = true;
		}
		
		WebMarkupContainer expwmc = new WebMarkupContainer("expHead");
		WebMarkupContainer eduwmc = new WebMarkupContainer("eduHead");
		add(expwmc);
		add(eduwmc);
		if(isEduTab)
			expwmc.setVisible(false);
		else
			eduwmc.setVisible(false);
		
		ListView<ProfessionalDetails> pdl = 
			new ListView<ProfessionalDetails>
				("proDet", new PropertyModel<List<ProfessionalDetails>>(this, "proDet")){

					private static final long serialVersionUID = -5857210572300208219L;

					@Override
					protected void populateItem(ListItem<ProfessionalDetails> item) {
						if(isEduTab)
							item.add(new EduDetFrag("proDevDiv",ProfessionalProfile.this,item.getModel()));
						else
							item.add(new ProDetFrag("proDevDiv", ProfessionalProfile.this,item.getModel()));						
					}
			
		};
		
		add(pdl);
	}
	
	public List<ProfessionalDetails> getProDet(){
		if(isEduTab)
			return new ProfessionalDetailsDAO(getSessionORMInterface()).getdegrees(super.userModel.getObject());
		else
			return new ProfessionalDetailsDAO(getSessionORMInterface()).getPositions(super.userModel.getObject());
	}
	
	public class ProDetFrag extends Fragment {

		private static final long serialVersionUID = -7841813921376958922L;
		private IModel<ProfessionalDetails> proDetModel;

		public ProDetFrag(String id, MarkupContainer markupProvider, IModel<ProfessionalDetails> model) {
			super(id, "proDetFrag", markupProvider, model);
			this.proDetModel = model;
			add(new Label("role", new PropertyModel<String>(model, "positionTitle")));
			add(new Label("org", new PropertyModel<String>(model, "orgName")));
			add(new Label("from", new PropertyModel<String>(this, "fromDate")));
			add(new Label("to", new PropertyModel<String>(this, "toDate")));
			add(new Label("jobSumm", new PropertyModel<String>(model, "description")).setEscapeModelStrings(false));
		}
		
		public String getFromDate(){
			if(proDetModel.getObject().getFromDate()==null)
				return null;
			return new SimpleDateFormat("MMM, yyyy").format(proDetModel.getObject().getFromDate());
		}

		public String getToDate(){
			if(proDetModel.getObject().getToDate()==null || proDetModel.getObject().getIsCurrent()){
				return "Present";
			}
			
			return new SimpleDateFormat("MMM, yyyy").format(proDetModel.getObject().getToDate());
		}

	}
	
	public class EduDetFrag extends Fragment {

		private static final long serialVersionUID = -7753526266062215744L;
		private IModel<ProfessionalDetails> eduDetModel;

		public EduDetFrag(String id, MarkupContainer markupProvider, IModel<ProfessionalDetails> model) {
			super(id, "eduDetFrag", markupProvider, model);
			this.eduDetModel = model;
			add(new Label("degree", new PropertyModel<String>(model, "positionTitle")));
			add(new Label("spl", new PropertyModel<String>(model, "specialization")){
				
				private static final long serialVersionUID = 5815245917015239585L;

				@Override
				public boolean isVisible() {
					return this.getDefaultModelObject()!=null && StringUtil.isValid(getDefaultModelObjectAsString());
				}
			});
			add(new Label("school", new PropertyModel<String>(model, "orgName")));
			add(new Label("from", new PropertyModel<String>(this, "fromDate")));
			add(new Label("to", new PropertyModel<String>(this, "toDate")));
			add(new Label("eduSumm", new PropertyModel<String>(model, "description")).setEscapeModelStrings(false));
		}
		
		public String getFromDate(){
			if(eduDetModel.getObject().getFromDate()==null)
				return null;
			return new SimpleDateFormat("MMM, yyyy").format(eduDetModel.getObject().getFromDate());
		}

		public String getToDate(){
			if(eduDetModel.getObject().getToDate()==null || eduDetModel.getObject().getIsCurrent()){
				return "Present";
			}
			
			return new SimpleDateFormat("MMM, yyyy").format(eduDetModel.getObject().getToDate());
		}

	}

	@Override
	public MenuItems getSelectedMenu() {
		String tabName = super.params.getString("1", RequestConstants.PARAM_USR_EXPERIENCES);
		if(tabName.equalsIgnoreCase(RequestConstants.PARAM_USR_EDUCATION)){
			isEduTab = true;
		}
		
		if(isEduTab)
			return MenuItems.EDUCATION;
		else 
			return MenuItems.EXPERIENCE;
	}

	

}
